//
//  CustomAlertView.swift
//  d11
//
//  Created by Wai Yin Chan on 3/10/2017.
//  Copyright © 2017 Tech_001. All rights reserved.
//

import Foundation
import UIKit

class AnnouncementAlertView: UIView, Modal {
    var backgroundView = UIView()
    var dialogView = UIView()
    
    var shouldDismissOnTapBackground: Bool = true
    
    convenience init(title:String, content:String, buttons: [UIButton]?, backgroundDismiss: Bool = true) {
        self.init(frame: UIScreen.main.bounds)
        
        self.shouldDismissOnTapBackground = backgroundDismiss
        
        backgroundView.frame = frame
        //mt
        backgroundView.backgroundColor = UIColor.clear
        backgroundView.alpha = 0
        addSubview(backgroundView)
        
        let dialogViewWidth = frame.width-64
        
        let titleLabel = UILabel(frame: CGRect(x: 4, y: 8, width: dialogViewWidth - 8, height: 28))
        titleLabel.font = UIFont(name: titleLabel.font.fontName, size: CGFloat(15))
        if title.count > 30{
            titleLabel.text=title.substring(to: title.index(title.startIndex,offsetBy:5))
        }else{
       
            titleLabel.text = title
            
        }
        
        titleLabel.textAlignment = .center
        dialogView.addSubview(titleLabel)
        let separatorLineView = UIView()
        separatorLineView.frame.origin = CGPoint(x: 16, y: titleLabel.frame.height + 8)
        separatorLineView.frame.size = CGSize(width: dialogViewWidth - 32, height: 1)
        separatorLineView.backgroundColor = UIColor.groupTableViewBackground
        dialogView.addSubview(separatorLineView)
        
        let contentLabel = UILabel()
        contentLabel.text = content
        contentLabel.numberOfLines = 0
        contentLabel.textAlignment = .center
        contentLabel.frame.origin = CGPoint(x: 20, y: separatorLineView.frame.height + separatorLineView.frame.origin.y)
        contentLabel.frame.size = CGSize(width: dialogViewWidth - 40 , height: UIHelper.heightForView(text: content, font: contentLabel.font, width: dialogViewWidth - 40) + 24)
        dialogView.addSubview(contentLabel)
        
        let buttonStack = TZStackView()
        if buttons != nil {
            buttonStack.axis = .horizontal
            buttonStack.distribution = .fillEqually
            buttonStack.frame.origin = CGPoint(x: 0, y: titleLabel.frame.height + 8 + separatorLineView.frame.height + 8 + contentLabel.frame.height)
            buttonStack.frame.size = CGSize(width: frame.width - 64, height: 40)
            
            for button in buttons! {
                buttonStack.addArrangedSubview(button)
            }
            dialogView.addSubview(buttonStack)
        }else{
            buttonStack.frame.size = CGSize(width: 0, height: 0)
        }
        
        let dialogViewHeight = titleLabel.frame.height + 8 + separatorLineView.frame.height + 8 + contentLabel.frame.height + buttonStack.frame.height
        dialogView.frame.origin = CGPoint(x: 32, y: frame.height)
        dialogView.frame.size = CGSize(width: frame.width-64, height: dialogViewHeight)
        dialogView.backgroundColor = UIColor.white
        dialogView.layer.cornerRadius = 12
        dialogView.layer.borderColor = UIColor.gray.cgColor
        dialogView.layer.borderWidth = 0.5
        dialogView.clipsToBounds = true
        
        addSubview(dialogView)
        
        if let alertButtons = buttons, alertButtons.count == 2 {
            let buttonSeparatorLineView = UIView()
            buttonSeparatorLineView.frame.origin = CGPoint(x: dialogViewWidth/2, y: buttonStack.frame.origin.y)
            buttonSeparatorLineView.frame.size = CGSize(width: 0.5, height: buttonStack.frame.height)
            buttonSeparatorLineView.backgroundColor = UIColor.init(white: 0.38, alpha: 1)
            dialogView.addSubview(buttonSeparatorLineView)
        }
        
        if self.shouldDismissOnTapBackground {
            backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTappedOnBackgroundView)))
        }
    }
    
    @objc func didTappedOnBackgroundView(){
        UIHelper.removeEffect()
        dismiss(animated: true)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
